const randomRange = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678";
function randomString(length){
  length = length || 8;
  var a = randomRange.length,n = "";
  for (var i = 0; i < length; i++) n += randomRange.charAt(Math.floor(Math.random() * a));
  return n;
};
const nowDate = (timestamp) => {
  let now = timestamp ? new Date(timestamp) : new Date()
  let year = now.getFullYear()
  const month = now.getMonth() + 1
  const day = now.getDate()
  return [year, month, day].map(formatNumber).join('-')
}
const nowDateTime = (timestamp) => {
  let date = timestamp ? new Date(timestamp) : new Date()
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
module.exports = {
  randomString,
  nowDate,
  nowDateTime
};